package com.bawei.library_log

import android.content.Context
import com.bawei.library_log.logger.EmailLogger
import com.bawei.library_log.logger.FileLogger
import com.bawei.library_log.logger.LogcatLogger
import com.bawei.library_log.logger.NetWorkLogger

/**
 * @Author : 
 * @Time : On 2023/6/20 10:26
 * @Description : Logger
 */
class Logger(private val TAG:String, private val level:LoggerLevel, private val loggerStrategy: LoggerStrategy, private val isDebug:Boolean, private val saveUrl:String,private val context: Context?){
    private var logger:ILogger //具体的logger
    init {
        logger = getLogger()
    }
    //创建对应的logger对象：策略者模式
    private fun getLogger():ILogger {
        logger =when(loggerStrategy){
           LoggerStrategy.LOGCAT -> LogcatLogger()
           LoggerStrategy.EMAIL -> EmailLogger()
           LoggerStrategy.FILE -> FileLogger()
           LoggerStrategy.NET -> NetWorkLogger()
       }
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)
        if(context != null){
            logger.setContext(context)
        }

        return logger

    }


    //次构造必须继承主构造
    constructor(builder:Builder):this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl,
        builder.context
    )


    /**
     * 输出Debug log
     */
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }

    /**
     * 输出Warn log
     */
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }

    /**
     * 输出Verbose log
     */
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }

    /**
     * 输出Error log
     */
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }
    /**
     * 输出Info log
     */
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }



    class Builder{
        var TAG:String = "默认TAG"
        var level:LoggerLevel = LoggerLevel.Debug
        var isDebug:Boolean = true
        var saveUrl:String = "默认路径"
        var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT //策略
        var context:Context? = null

        //链式编程
        fun setTAG(TAG:String):Builder{
            this.TAG = TAG
            return this
        }
        fun setLevel(level:LoggerLevel):Builder{
            this.level = level
            return this
        }
        fun setLoggerStrategy(loggerStrategy: LoggerStrategy):Builder{//设置策略
            this.loggerStrategy = loggerStrategy
            return this
        }

        fun isDebug(isDebug: Boolean):Builder{
            this.isDebug = isDebug
            return this
        }
        fun setSaveUrl(saveUrl: String):Builder{
            this.saveUrl = saveUrl
            return this
        }
        fun setContext(context: Context):Builder{
            this.context = context
            return this
        }
        //构建一个Logger对象：构造Builder
        fun build():Logger{
            return Logger(this)
        }




    }

}